/**
 * This class is a starting point for Pivot Table Wizard
 */
package slhs.tempo.pivotTableUtilities;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;

import org.openswing.swing.util.client.ClientUtils;
import org.openswing.swing.wizard.client.WizardPanel;

import slhs.tempo.TableAnalyzer.TablePanelHandler;

/**
 * CreatePivotTables.java
 * 
 * @author Swapnil Devikar
 * 
 */
public class PivotTableWizard extends JFrame implements ActionListener {

    private static final String WINDOW_TITLE = "Pivot Table Wizard";
    private WizardPanel wizardPanel;
    private PivotTableWizardController controller;

    private PivotWizardFirstPanel firstPanel;
    private PivotWizardSecondPanel secondPanel;
    private PivotWizardAnalysisPanel thirdPanel;
    private PivotWizardPostAnalysisFilterPanel fourthPanel;
    private PivotWizardPostAnalysisPivotPanel fifthPanel;

    private final TablePanelHandler tablePanelHandlerObject;

    public PivotTableWizard(TablePanelHandler tablePanelHandler) {
	this.tablePanelHandlerObject = tablePanelHandler;
	this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	super.setTitle(WINDOW_TITLE);
	setSize(820, 400);
	System.out.println("Pivot Wizard started");
	initComponents();
	addComponents();
	this.setVisible(true);

	ClientUtils.centerFrame(this);

    }

    /**
     * Helper method to initialize components
     * */
    private void initComponents() {
	wizardPanel = new WizardPanel();
	firstPanel = new PivotWizardFirstPanel(wizardPanel);
	secondPanel = new PivotWizardSecondPanel(tablePanelHandlerObject, wizardPanel);
	thirdPanel = new PivotWizardAnalysisPanel();
	fourthPanel = new PivotWizardPostAnalysisFilterPanel();
	fifthPanel = new PivotWizardPostAnalysisPivotPanel();

	controller = new PivotTableWizardController(wizardPanel);

    }

    /**
     * helper method to add components
     */
    private void addComponents() {

	wizardPanel.addPanel(firstPanel);
	wizardPanel.addPanel(secondPanel);
	wizardPanel.addPanel(thirdPanel);
	wizardPanel.addPanel(fourthPanel);
	wizardPanel.addPanel(fifthPanel);

	try {
	    this.getContentPane().add(wizardPanel, BorderLayout.CENTER);
	} catch (Exception e) {
	    e.printStackTrace();
	}

	// configure components
	wizardPanel.addActionListener(this);
	firstPanel.addNextRadioButtonItemListener(controller);
	wizardPanel.setNavigationLogic(controller);
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    @Override
    public void actionPerformed(ActionEvent e) {

	Object source = e.getSource();

	String screenId = wizardPanel.getCurrentVisiblePanel().getPanelId();

	// First panel and cancel button is hit
	if (source.equals(wizardPanel.getCancelButton()) && screenId.equals(PivotWizardFirstPanel.SCREEN_ID)) {
	    this.dispose();
	}
	// cancel/finish button hit on panel other than first panel
	else if (source.equals(wizardPanel.getNextButton())
	/* && !wizardPanel.getCurrentVisiblePanel().getPanelId().equals(PivotWizardFirstPanel.SCREEN_ID) */) {
	    new ExecutionEngine(wizardPanel, PivotTableWizard.this);
	}
    }

}
